<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 生成编码
//-------------------------

namespace app\admin\controller;

use app\admin\Controller;
use think\Db;
use think\Lang;
use think\Loader;
use think\Exception;

class Generalcode extends Controller
{
    use \app\admin\traits\controller\Controller;

    /**
     * 列表
     */
    public function index()
    {
        $this->view->assign('todaydate' , date("ymd"));
        return $this->view->fetch();
    }
    
    /**
     * 生成code
     */
    public function export()
    {
        $newcodearr = array();
        $title  = $this->request->param("title");
        $num  = $this->request->param("num");
        $code = array(
                    "ppmc" => $this->request->param("ppmc"),
                    "sccj" => $this->request->param("sccj"),
                    "cpxh" => $this->request->param("cpxh"),
                    "yanse" => $this->request->param("yanse"),
                    "chicun" => $this->request->param("chicun"),
                    "scriqi" => $this->request->param("scriqi") 
                    );
        $code['ppmc'] = $code['ppmc'] . "-";                    
        $code['sccj'] = $code['sccj'] . "-";                    
        $newcode = implode("", $code); //生成编码的抬头
        for($i = 0; $i < $num ; $i++){
             $randcode = str_pad(mt_rand(0, 10000000),7,"0",STR_PAD_LEFT);
             $newcodetmp['code'] = strtoupper($newcode.$randcode);
             $newcodetmp['title'] = $title;
             $newcodearr[] = $newcodetmp;
        }
        Db::table('tp_security_code')->insertAll($newcodearr);
        return ajax_return_adv("导入成功!","current","",false,"/admin/import/index.html");
        
    }

}